<?php

/**
 * 版本分布
 *
 * @author genjie.wang
 */

require_once 'CommonController.php';

class Admin_ClientVersionDataController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_ClientVersionData();
        $this->view->title = "管理中心";
    }

    /**
     * 版本分布
     * 
     * @return void
     */
    public function indexAction()
    {
        $clientVersionDataModel = new Application_Model_ClientVersionData();
        $data["osName"] = $this->_request->getpost('osName');
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $query = "SELECT osName AS os FROM {$clientVersionDataModel->getTableName()} WHERE productCode='{$productCode}' AND  `date`='" . date('Y-m-d',strtotime('-1 day')) . "' GROUP BY osName ORDER BY osName DESC";
        $this->view->osnameall = $this->_model->getAdapter()->fetchAll($query);
         if ($data["osName"] !== NULL) {
            $this->view->osName = $os = $data["osName"];
            $where = " AND `osName` = '" . $os . "' ";
            $where = "productCode = '{$productCode}' AND  `date`='" . date("Y-m-d",strtotime("-1 day")) . "'" . $where;
            $this->view->version = $clientVersionDataModel->fetchAll($where, "id desc");
            $cnt["currentBootAllClientNum"] = 0;
            $this->view->cnt = array("currentBootAllClientNum"=>0, "dayBootClientNum"=>0, "recentBootClientNum"=>0, "recentBootClientNum2"=>0);
            foreach($this->view->version as $row){
            //var_dump($row["currentBootAllClientNum"]);
                $this->view->cnt["currentBootAllClientNum"] += $row["currentBootAllClientNum"];
                $this->view->cnt["dayBootClientNum"] += $row["dayBootClientNum"];
                $this->view->cnt["recentBootClientNum"] += $row["recentBootClientNum"];
                $this->view->cnt["recentBootClientNum2"] += $row["recentBootClientNum2"];
            }
         } else {
            if(!empty($this->view->osnameall[0]['os']))
                $this->view->osName = $os = $this->view->osnameall[0]['os'];
            else 
                $this->view->osName = $os ="";
            $where = " AND `osName` = '" . $os . "' ";
            $where = "productCode = '{$productCode}' AND  `date`='" . date("Y-m-d",strtotime("-1 day")) . "'" . $where;
            $this->view->version = $clientVersionDataModel->fetchAll($where, "id desc");
            $cnt["currentBootAllClientNum"] = 0;
            $this->view->cnt = array("currentBootAllClientNum"=>0, "dayBootClientNum"=>0, "recentBootClientNum"=>0, "recentBootClientNum2"=>0);
            foreach($this->view->version as $row){
            //var_dump($row["currentBootAllClientNum"]);
                $this->view->cnt["currentBootAllClientNum"] += $row["currentBootAllClientNum"];
                $this->view->cnt["dayBootClientNum"] += $row["dayBootClientNum"];
                $this->view->cnt["recentBootClientNum"] += $row["recentBootClientNum"];
                $this->view->cnt["recentBootClientNum2"] += $row["recentBootClientNum2"];
            }
            //$this->view->version = array();
            //$this->view->count = array();
         }
        $this->render();
    }
}